﻿@{
	Layout = "~/Views/Shared/_Layout.cshtml";
}

@using Hangerd.Mvc.Extensions;

@model dPet.Web.Hospital.ViewModels.ProductModel

<div class="page-header">
	<h3>编辑商品信息</h3>
</div>

@using (Html.BeginForm("SaveProduct", "Product", FormMethod.Post, new { onsubmit = "return check_form()" }))
{
	<div class="panel panel-default">
		<div class="panel-heading">
			&nbsp;
		</div>

		<div class="panel-body form-horizontal">
			@Html.Hidden("Id", Model.Product.Id)
			@Html.Hidden("HospitalId", Model.Product.HospitalId)

			<div class="form-group">
				<label for="Barcode" class="col-md-1 control-label">条　　码</label>
				<div class="col-md-3">
					@Html.TextBox("Barcode", Model.Product.Barcode, new { @class = "form-control", placeholder = "点击此处进行扫码", onfocus = "showScanBarcodePanel()" })
				</div>
			</div>

			<div class="form-group">
				<label for="Name" class="col-md-1 control-label">名　　称</label>
				<div class="col-md-3">
					@Html.TextBox("Name", Model.Product.Name, new { @class = "form-control", placeholder = "请输入商品名称" })
				</div>
			</div>

			<div class="form-group">
				<label for="CategoryId" class="col-md-1 control-label">大　　类</label>
				<div class="col-md-3">
					@Html.DropDownList("Category.Id",
					new SelectList(Model.Categories, "Id", "Name", Model.Product.Category != null ? Model.Product.Category.Id : string.Empty),
					new { @class = "form-control", onchange = "category_changed()" })
				</div>
			</div>

			<div class="form-group">
				<label for="SubCategoryId" class="col-md-1 control-label">小　　类</label>
				<div class="col-md-3">
					<select id="SubCategory_Id" name="SubCategory.Id" class="form-control">
						<option value="">无</option>
						@foreach (var subCategory in Model.SubCategories)
						{
							<option value="@subCategory.Id" @Html.IsSelected(Model.Product.SubCategory != null && Model.Product.SubCategory.Id == subCategory.Id)>@subCategory.Name</option>
						}
					</select>
				</div>
			</div>

			<div class="form-group">
				<label for="UnitPrice" class="col-md-1 control-label">单　　价</label>
				<div class="col-md-3">
					@Html.TextBox("UnitPrice", Model.Product.UnitPrice, new { @class = "form-control", placeholder = "请输入商品单价" })
				</div>
			</div>

			<div class="form-group">
				<label for="QueryTag" class="col-md-1 control-label">查询标签</label>
				<div class="col-md-3">
					@Html.TextBox("QueryTag", Model.Product.QueryTag, new { @class = "form-control", placeholder = "请输入商品标签" })
				</div>
			</div>

			<div class="form-group">
				<label for="UnitPrice" class="col-md-1 control-label">单　　位</label>
				<div class="col-md-3">
					<select name="UnitName" class="form-control">
						<option value="件" @Html.IsSelected(Model.Product.UnitName == "件")>件</option>
						<option value="个" @Html.IsSelected(Model.Product.UnitName == "个")>个</option>
						<option value="盒" @Html.IsSelected(Model.Product.UnitName == "盒")>盒</option>
						<option value="箱" @Html.IsSelected(Model.Product.UnitName == "箱")>箱</option>
					</select>
				</div>
			</div>

			<div class="form-group">
				<label for="QuantityAlarmThreshold" class="col-md-1 control-label">数量告警</label>
				<div class="col-md-3">
					<div class="input-group">
						<span class="input-group-addon">
							阈值（数量）
						</span>
						@Html.TextBox("QuantityAlarmThreshold", Model.Product.QuantityAlarmThreshold, new { @class = "form-control", placeholder = "请输入库存数量告警阈值" })
						<span class="input-group-addon">
							启用 @Html.CheckBox("EnableQuantityAlarm", Model.Product.EnableQuantityAlarm)
						</span>
					</div>
				</div>
			</div>

			<div class="form-group">
				<label for="ExpiryAlarmThreshold" class="col-md-1 control-label">过期告警</label>
				<div class="col-md-3">
					<div class="input-group">
						<span class="input-group-addon">
							阈值（天数）
						</span>
						@Html.TextBox("ExpiryAlarmThreshold", Model.Product.ExpiryAlarmThreshold, new { @class = "form-control", placeholder = "请输入库存过期告警阈值" })
						<span class="input-group-addon">
							启用 @Html.CheckBox("EnableExpiryAlarm", Model.Product.EnableExpiryAlarm)
						</span>
					</div>
				</div>
			</div>

			<div class="form-group">
				<div class="col-md-offset-1 col-sm-10">
					<button type="button" class="btn btn-default btn-sm" onclick="window.history.go(-1);"><span class="glyphicon glyphicon-chevron-left"></span> 返 回</button>
					<button type="submit" class="btn btn-info btn-sm"><span class="glyphicon glyphicon-floppy-disk"></span> 保 存</button>
				</div>
			</div>
		</div>
	</div>
}

<div class="modal fade" id="scan_Modal" tabindex="-1" role="dialog" aria-hidden="true">
	<br /><br /><br /><br />
	<div class="modal-dialog modal-sm">
		<div class="modal-content">
			<form onsubmit="fillProductBarcode()">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title">读取条码</h4>
				</div>
				<div class="modal-body">
					<input id="txt_scaned_barcode" type="text" class="form-control" placeholder="读取条码" ng-model="scanedBarcode" />
				</div>
			</form>
		</div>
	</div>
</div>

@section scripts {
	<script type="text/javascript">
		$(document).ready(function () {
			set_left_nav('#ln_ProductList');
		});

		function check_form() {
			if ($.trim($("#Name").val()) == '') {
				alert("请输入商品名称");
				$("#Name").focus();
				return false;
			}

			if ($.trim($("#UnitPrice").val()) == '') {
				alert("请输入商品单价");
				$("#UnitPrice").focus();
				return false;
			}

			return true;
		}

		function category_changed() {
			var categoryId = $("#Category_Id").val();

			if (categoryId != '') {
				$.post("/Ajax/ProductSubCategoryList?categoryId=" + categoryId, function (data) {
					var subCategoryList = $("#SubCategory_Id").empty();

					subCategoryList.append("<option value=''>无</option>");

					for (var i in data) {
						subCategoryList.append("<option value='" + data[i].Id + "'>" + data[i].Name + "</option>");
					}
				});
			}
		}

		function showScanBarcodePanel() {
			$('#scan_Modal').modal('show');
		}

		$('#scan_Modal').on('shown.bs.modal', function (e) {
			$("#txt_scaned_barcode").val('');
			$('#txt_scaned_barcode').focus();
		});

		function fillProductBarcode() {
			var scanedBarcode = $.trim($("#txt_scaned_barcode").val());

			if (scanedBarcode != '') {
				$("#Barcode").val(scanedBarcode);
				$('#scan_Modal').modal('hide');
			}
		}
	</script>

	@if (string.IsNullOrWhiteSpace(Model.Product.Id))
	{
		<script type="text/javascript">
			showScanBarcodePanel();
		</script>
	}
}